package com.haixiaoke.smartLock.huixiangjia;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.haixiaoke.common.exception.ServiceException;
import com.haixiaoke.common.utils.RandomNoUtils;
import com.haixiaoke.common.utils.StringUtils;
import com.haixiaoke.domain.*;
import com.haixiaoke.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 慧享佳接口事件处理
 * xiaolong
 * 2024-08-14
 */
@Service
public class SmartLockHxjEventHandleService {
    @Autowired
    private SmartLockServiceImpl smartLockService;
    @Autowired
    private SmartLockHxjService smartLockHxjService;
    @Autowired
    private SmartLockPwdServiceImpl smartLockPwdService;
    @Autowired
    private SmartLockFaceServiceImpl smartLockFaceService;
    @Autowired
    private SmartLockCardServiceImpl smartLockCardService;
    @Autowired
    private SmartLockBluetoothServiceImpl smartLockBluetoothService;

    /**
     * 注册设备
     *
     * @param smartLock 智能锁设备信息
     * @return
     */
    public int insertSmartLock(SmartLock smartLock) {
        String result = smartLockHxjService.getSmartLock(smartLock);
        if ("0".equals(result)) {
            throw new ServiceException("注册失败，请检查设备SN是否正确");
        }
        smartLock.setBattery(Integer.valueOf(result));
        return smartLockService.insertSmartLock(smartLock);
    }

    /**
     * 获取智能锁临时密码权限信息
     *
     * @param sn 智能锁SN
     * @return
     */
    public List<SmartLockPwd> getLockPassword(String sn) {
        return smartLockPwdService.selectSmartLockPwdBySN(sn);

    }

    /**
     * 新增智能锁密码授权信息和智能锁密码授权
     *
     * @param smartLockPwd 智能锁密码授权信息
     * @return 结果
     */
    public int insertSmartLockPwd(SmartLockPwd smartLockPwd) {
        if (StrUtil.isEmpty(smartLockPwd.getSn())) {
            throw new ServiceException("智能锁sn号不能为空");
        }
        if (smartLockPwd.getStartTime() == null) {
            throw new ServiceException("开始时间不能为空");
        }
        if (smartLockPwd.getEndTime() == null) {
            throw new ServiceException("结束时间不能为空");
        }
        if (smartLockPwd.getGenerateType() == null) {
            throw new ServiceException("请选择密码生成方式");
        }

        if (SmartLockPwd.PASSWORD_GENERATE_TYPE_SYSGEN.equals(smartLockPwd.getGenerateType())) {
            smartLockPwd.setPassword(RandomNoUtils.getRandomNum(6));
        } else {
            if (StrUtil.isEmpty(smartLockPwd.getPassword())) {
                throw new ServiceException("密码不能为空");
            }
            if (smartLockPwd.getPassword().length() != 6 || !StringUtils.isNumeric(smartLockPwd.getPassword())) {
                throw new ServiceException("请输入6位数字密码");
            }
        }

        SmartLock smartLock = new SmartLock();
        smartLock.setSn(smartLockPwd.getSn());
        smartLock.setPassword(smartLockPwd.getPassword());
        smartLock.setStartTime(smartLockPwd.getStartTime());
        smartLock.setEndTime(smartLockPwd.getEndTime());
        smartLock.setIdentification(smartLockPwd.getIdentification());
        return Integer.parseInt(smartLockHxjService.addPassword(smartLock));
    }


    /**
     * 删除智能锁密码授权信息和删除密码授权
     *
     */
    public int deleteSmartLockPwd(String sn, String identification) {
        SmartLock smartLock = new SmartLock();
        smartLock.setSn(sn);
        smartLock.setIdentification(identification);
        SmartLockPwd smartLockPwd = smartLockPwdService.selectSmartLockPwdBySNAndIdentification(sn, identification);
        if (ObjectUtil.isEmpty(smartLockPwd)) {
            throw new ServiceException("该授权信息不存在");
        }
        smartLock.setLockIdentifier(smartLockPwd.getLockIdentifier());
        return Integer.parseInt(smartLockHxjService.deletePwd(smartLock));
    }

    /**
     * 新增智能锁授权人脸信息
     *
     * @param smartLockFace 智能锁授权人脸信息
     * @return 结果
     */
    public int insertSmartLockFace(SmartLockFace smartLockFace) {
        if (StrUtil.isEmpty(smartLockFace.getSn())) {
            throw new ServiceException("智能锁sn号不能为空");
        }
        if (StrUtil.isEmpty(smartLockFace.getFaceImage())) {
            throw new ServiceException("请上传人脸照");
        }

        if (SmartLock.PERMANENT_FLAG_NO.equals(smartLockFace.getPasswordType())) {
            if (smartLockFace.getStartTime() == null) {
                throw new ServiceException("开始时间不能为空");
            }
            if (smartLockFace.getEndTime() == null) {
                throw new ServiceException("结束时间不能为空");
            }
        }


        SmartLock smartLock = new SmartLock();
        smartLock.setSn(smartLockFace.getSn());
        smartLock.setFaceImage(smartLockFace.getFaceImage());
        smartLock.setStartTime(smartLockFace.getStartTime());
        smartLock.setEndTime(smartLockFace.getEndTime());
        smartLock.setPasswordType(smartLockFace.getPasswordType());
        smartLock.setIdentification(smartLockFace.getIdentification());
        return Integer.parseInt(smartLockHxjService.addFaceImage(smartLock));
    }

    /**
     * 删除智能锁人脸授权信息和删除人脸授权
     * @return
     */
    public int deleteSmartLockFace(String sn, String identification) {
        SmartLock smartLock = new SmartLock();
        smartLock.setSn(sn);
        smartLock.setIdentification(identification);
        SmartLockFace smartLockFace = smartLockFaceService.selectSmartLockPwdBySNAndIdentification(sn, identification);
        if (ObjectUtil.isEmpty(smartLockFace)) {
            throw new ServiceException("该授权信息不存在");
        }
        smartLock.setLockIdentifier(smartLockFace.getLockIdentifier());
        return Integer.parseInt(smartLockHxjService.deleteFace(smartLock));
    }

    /**
     * 删除智能锁设备
     */
    @Transactional
    public int deleteSmartLockByLockId(SmartLock smartLock) {
        String sn = smartLock.getSn();
        String result = smartLockHxjService.deleteAll(sn);
        if (!"1".equals(result)) {
            throw new ServiceException(result);
        }
        smartLockCardService.deleteSmartLockCardBySn(smartLock.getSn());
        smartLockFaceService.deleteSmartLockFaceBySn(smartLock.getSn());
        smartLockPwdService.deleteSmartLockPwdBySn(smartLock.getSn());
        return smartLockService.deleteSmartLockByLockId(smartLock.getLockId());
    }

    /**
     * 新增智能锁门卡授权信息和授权门卡
     */
    public int insertSmartLockCard(SmartLockCard smartLockCard) {
        if (StrUtil.isEmpty(smartLockCard.getSn())) {
            throw new ServiceException("智能锁sn号不能为空");
        }
        if (smartLockCard.getStartTime() == null) {
            throw new ServiceException("开始时间不能为空");
        }
        if (smartLockCard.getEndTime() == null) {
            throw new ServiceException("结束时间不能为空");
        }
        if (smartLockCard.getCardType() == null) {
            throw new ServiceException("请选择授权门卡类型");
        }
        if (SmartLockCard.IDENTITY_CARD_TYPE.equals(smartLockCard.getCardType())) {
            if (smartLockCard.getCardNumber().length() != 16) {
                throw new ServiceException("卡号输入错误");
            }
        } else {
            throw new ServiceException("暂无普通门卡授权功能");
        }
        SmartLock smartLock = new SmartLock();
        smartLock.setSn(smartLockCard.getSn());
        smartLock.setStartTime(smartLockCard.getStartTime());
        smartLock.setEndTime(smartLockCard.getEndTime());
        smartLock.setCardType(smartLockCard.getCardType());
        smartLock.setCardNumber(smartLockCard.getCardNumber());
        smartLock.setIdentification(smartLockCard.getIdentification());
        return Integer.parseInt(smartLockHxjService.addCard(smartLock));
    }

    /**
     * 获取智能锁门卡信息
     *
     * @param sn 智能锁SN
     * @return
     */
    public List<SmartLockCard> selectLockCard(String sn) {
        return smartLockCardService.selectSmartLockCardBySN(sn);
    }

    /**
     * 删除智能锁门卡授权信息和授权门卡授权
     *
     * @return
     */
    public int deleteSmartLockCard(SmartLockCard smartLockCard) {
        SmartLock smartLock = new SmartLock();
        smartLock.setSn(smartLockCard.getSn());
        smartLock.setIdentification(smartLockCard.getIdentification());
        SmartLockCard lockCard = smartLockCardService.selectSmartLockPwdBySNAndIdentificationAndCardType(smartLock.getSn(), smartLock.getIdentification(), SmartLockCard.IDENTITY_CARD_TYPE);
        if (ObjectUtil.isEmpty(lockCard)) {
            throw new ServiceException("该门卡授权信息不存在");
        }
        smartLock.setLockIdentifier(lockCard.getLockIdentifier());
        return Integer.parseInt(smartLockHxjService.deleteCard(smartLock));
    }


    /**
     * 新增蓝牙授权信息和授权蓝牙开锁
     *
     * @param smartLockBluetooth 蓝牙授权信息
     * @return 结果
     */
    public int insertSmartLockBluetooth(SmartLockBluetooth smartLockBluetooth) {
        if (StrUtil.isEmpty(smartLockBluetooth.getPhonenumber())) {
            throw new ServiceException("手机号不能为空");
        }
        if (StrUtil.isEmpty(smartLockBluetooth.getSn())) {
            throw new ServiceException("智能锁sn号不能为空");
        }
        SmartLock smartLock = new SmartLock();
        smartLock.setSn(smartLockBluetooth.getSn());
        //查询蓝牙开锁密钥
        String resultBean = smartLockHxjService.getUnlockKey(smartLock);
        if ("0".equals(resultBean)) {
            throw new ServiceException("获取蓝牙开锁密钥失败");
        }
        String bluetoothCookie = (String) JSONObject.parseObject(resultBean).get("aesKey");
        String bluetoothMac = (String) JSONObject.parseObject(resultBean).get("lockMac");
        String bluetoothPassword = (String) JSONObject.parseObject(resultBean).get("generalAuthCode");
        smartLockBluetooth.setBluetoothCookie(bluetoothCookie);
        smartLockBluetooth.setBluetoothMac(bluetoothMac);
        smartLockBluetooth.setBluetoothPwd(bluetoothPassword);
        smartLockBluetooth.setIdentification(smartLockBluetooth.getPhonenumber());
        return smartLockBluetoothService.insertSmartLockBluetooth(smartLockBluetooth);
    }


    /**
     * 删除智能锁蓝牙权限
     *
     * @param sn     智能锁SN
     * @param mobile 蓝牙标识
     * @return
     */
    public int deleteBle(String sn, String mobile) {
        return smartLockBluetoothService.deleteSmartLockBluetoothBySnAndIdentification(sn, mobile);
    }


    /**
     * 获取蓝牙授权信息
     * @param sn
     * @return
     */
    public List<SmartLockBluetooth> getBluetooth(String sn) {
        return smartLockBluetoothService.selectSmartLockBluetoothBySN(sn);
    }
}
